/*
 * Copyright 2010 by IPS. Floor 3,Universal Industrial Building, 
 * Tian Yaoqiao Road 1178,Shanghai, P.R. China，200300. All rights reserved.
 *
 * This software is the confidential and proprietary information of IPS
 * ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with IPS.
 */
package com.axis2.web.filter;

import com.axis2.common.util.SessionUtil;
import org.slf4j.MDC;

import javax.servlet.*;
import java.io.IOException;

/**
 * MDCSessionFilter
 * 
 * @author yemulin
 * @version 1.0 2013-2-25
 */
public class MdcSessionFilter implements Filter {
	
	private String TRACE_KEY  ;
	
	public void init(FilterConfig filterConfig) throws ServletException {
		// get trace key
		TRACE_KEY = filterConfig.getInitParameter("TRACE_KEY");
		if(TRACE_KEY==null||"".equals(TRACE_KEY)){
			TRACE_KEY="sessionId";
		}
	}
	
	/**
	 * log trace filter
	 * 
	 * @param request
	 * @param response
	 * @param chain
	 * @throws IOException
	 * @throws ServletException
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {

		try {
			// trace no
			MDC.put(TRACE_KEY, SessionUtil.getSessionId());

			// Continue processing the rest of the filter chain.
			chain.doFilter(request, response);
		} finally {
			// remove trace no
			MDC.remove(TRACE_KEY);
		}
	}

	public void destroy() {
	}
}

